//
//  UIViewController+LLPanBack.m
//  TaoTuo
//
//  Created by admin on 2019/11/14.
//  Copyright © 2019 admin. All rights reserved.
//

#import "UIViewController+LLPanBack.h"

@implementation UIViewController (LLPanBack)

- (void)addPanGestureRecognizerToView:(UIScrollView *)scrollView
{
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] init];
    pan.delegate = self;
    [scrollView addGestureRecognizer:pan];
    
    // Forward the gesture events to the private handler of the onboard gesture recognizer.
    NSArray *internalTargets = [self.navigationController.interactivePopGestureRecognizer valueForKey:@"targets"];
    id internalTarget = [internalTargets.firstObject valueForKey:@"target"];
    SEL internalAction = NSSelectorFromString(@"handleNavigationTransition:");
    [pan addTarget:internalTarget action:internalAction];
    // 禁用系统的侧滑返回
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer
{
    if (self.navigationController.viewControllers.count <= 1) {
        return NO;
    }
    // 当导航控制器当前处于过渡状态时不处理
    if ([[self.navigationController valueForKey:@"_isTransitioning"] boolValue]) {
        return NO;
    }
    // 偏移量大于0不处理
    UIScrollView *scrollView = (UIScrollView *)gestureRecognizer.view;
    if (scrollView.contentOffset.x > 0) {
        return NO;
    }
    
    // 向右滑动不处理
    CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view];
    if (translation.x <= 0) {
        return NO;
    }
    
    return YES;
}



@end
